//
//  FMAudioPlayer.m
//  AudioProgress
//
//  Created by 赵成峰 on 2018/8/31.
//  Copyright © 2018年 赵成峰. All rights reserved.
//

#import "FMAudioPlayer.h"
#import "FMAudioPlayer.h"
#include "fmod_player.hpp"

@implementation FMAudioPlayerOption

+ (NSArray<FMAudioPlayerOption *> *)defaultOptions
{
    NSMutableArray *array = [NSMutableArray array];
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"原声";
        option.flag = FMAudioPlayerOptionFlagOriginal;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"萝莉";
        option.flag = FMAudioPlayerOptionFlagLolita;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"惊悚";
        option.flag = FMAudioPlayerOptionFlagThriller;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"大叔";
        option.flag = FMAudioPlayerOptionFlagUncle;
        [array addObject:option];
    }    
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"搞怪";
        option.flag = FMAudioPlayerOptionFlagFunny;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"空灵";
        option.flag = FMAudioPlayerOptionFlagVacant;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"机器人";
        option.flag = FMAudioPlayerOptionFlagRobot;
        [array addObject:option];
    }
    
    {
        FMAudioPlayerOption *option = [[FMAudioPlayerOption alloc] init];
        option.name = @"合唱";
        option.flag = FMAudioPlayerOptionFlagChorus;
        [array addObject:option];
    }
    return array;
}

@end

@interface FMAudioPlayer ()

@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) FMAudioPlayerOption *option;

@end

@implementation FMAudioPlayer
{
    FPlayer *_player;
}

- (instancetype)initWithPath:(NSString *)path option:(FMAudioPlayerOption *)option
{
    if (self = [super init]) {
        self.path = path;
        self.option = option;
        play_init(&_player);
    }
    return self;
}

- (instancetype)initWithPath:(NSString *)path 
{
    if (self = [super init]) {
        self.path = path;
        play_init(&_player);
    }
    return self;
}

- (void)dealloc
{
    play_close(_player);
}

- (void)play
{
    play_start(_player, [self.path UTF8String], self.option.flag);
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(checkIsPlaying) object:nil];
    [self performSelector:@selector(checkIsPlaying) withObject:nil afterDelay:0.1];
}

- (void)setPitch:(CGFloat)pitch {
    _pitch = pitch;
}

- (void)setType:(NSInteger)type {
    _type = type;
}

- (void)play_ready
{
    play_ready(_player, [self.path UTF8String]);
}

- (void)save:(NSString *)outPath pitch:(CGFloat)pitch complete:(void (^)(BOOL success))success {
    self.pitch = pitch;
    int err = play_save(_player, [self.path UTF8String], [outPath UTF8String], pitch);
    if (err == 0) {
        success(YES);
    } else {
        success(NO);
    }
}

- (void)save:(NSString *)outPath volume:(CGFloat)volume complete:(void (^)(BOOL success))success {
    int err = play_save_volume(_player, [self.path UTF8String], [outPath UTF8String], volume);
    if (err == 0) {
        success(YES);
    } else {
        success(NO);
    }
}

- (void)save:(NSString *)outPath type:(NSInteger)type complete:(void (^)(BOOL success))success {
    int err = play_save_style(_player, [self.path UTF8String], [outPath UTF8String], (int)type);
    if (err == 0) {
        success(YES);
    } else {
        success(NO);
    }
}


- (void)pause {
    play_continue_pause(_player, 1);
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(checkIsPlaying) object:nil];
}

- (void)startWithPitch {
    //设置音效
    play_set_pitch(_player, self.pitch);
    [self start];
}

- (void)startWithStyle {
    //设置音效
    play_set_style(_player, self.type);
    [self start];
}

- (void)start {
    play_continue_pause(_player, 0);
    [self performSelector:@selector(checkIsPlaying) withObject:nil afterDelay:0.05];
}

- (void)stop {
    play_stop(_player);
}

- (void)checkIsPlaying {
    if (play_is_playing(_player)) {
        NSLog(@"播放结束");
        [self stop];
        [self play_ready];
        if ([self.delegate respondsToSelector:@selector(timeRunScale:)]) {
            [self.delegate timeRunScale:1.0];
        }
        if ([self.delegate respondsToSelector:@selector(trackPlayDidEnd)]) {
            [self.delegate trackPlayDidEnd];
        }
    } else {
        [self performSelector:@selector(checkIsPlaying) withObject:nil afterDelay:0.05];
        NSLog(@"正在播放 当前播放位置:%d",play_get_current_pos(_player));
        if ([self.delegate respondsToSelector:@selector(timeRunScale:)]) {
            [self.delegate timeRunScale:(CGFloat)play_get_current_pos(_player)/play_get_total_seconds(_player)];
        }
    }
}

- (int)get_total_seconds {
    return play_get_total_seconds(_player, [self.path UTF8String]);
}

- (void)set_current_pos:(int)position {
    play_set_current_pos(_player, position);
}

- (void)set_volume:(float)volume {
    play_set_volume(_player, volume);
}

@end
